Skip to content

Clarify tag-only YamlDerivedType does not set default derived type#134

Merged
xoofx merged 1 commit into
xoofx:mainfrom
fdcastel:feature/tag-only-derived-type-default
Mar 29, 2026
Merged

Clarify tag-only YamlDerivedType does not set default derived type#134
xoofx merged 1 commit into
xoofx:mainfrom
fdcastel:feature/tag-only-derived-type-default

Conversation

@fdcastel
Copy link
Copy Markdown
Contributor

Motivation

When using YamlDerivedType(Type) or YamlDerivedTypeAttribute(Type) with only a Tag (no discriminator), the constructor doc comment stated the entry would be "the default when no discriminator matches." This was misleading — the actual behavior correctly treats tag-only entries as tag dispatch participants only, not as the default derived type.

Scenario

DerivedTypeMappings =
{
    [typeof(PullMonitorConfiguration)] =
    [
        new YamlDerivedType(typeof(CheckInMonitorConfiguration)) { Tag = \"!checkin\" },
        new YamlDerivedType(typeof(DnsMonitorConfiguration)) { Tag = \"!dns\" },
    ]
}

With this configuration, an untagged YAML mapping should deserialize as the base type PullMonitorConfiguration, not as CheckInMonitorConfiguration.

Changes

  1. Clarified doc comments on both YamlDerivedType(Type) and YamlDerivedTypeAttribute(Type) constructors to explicitly document that:

    • Default is set only when both Discriminator and Tag are null
    • When Tag is set, the entry participates in tag-based dispatch only
  2. Added 10 comprehensive tests covering:

    • Attribute-based tag-only entries: untagged values deserialize as base type
    • Runtime tag-only entries (DerivedTypeMappings): untagged values deserialize as base type
    • Dictionary with mixed tagged and untagged values
    • Explicit default type coexisting with tag-only entries
    • Roundtrip serialization for both attribute and runtime paths

Update doc comments on YamlDerivedType(Type) and YamlDerivedTypeAttribute(Type)
constructors to clearly document that a tag-only entry (Discriminator=null,
Tag!=null) does NOT become the default derived type.

The default is only set when both Discriminator and Tag are null.

Add 10 comprehensive tests covering:
- Attribute-based tag-only entries: untagged values deserialize as base type
- Runtime tag-only entries: untagged values deserialize as base type
- Dictionary with mixed tagged/untagged values
- Explicit default with tag-only entries coexisting
- Roundtrip serialization for both attribute and runtime paths
@fdcastel
Copy link
Copy Markdown
Contributor Author

P.S.: This is a clarification-only issue. Feel free to request any changes (i.e. "keep only the docs", "modify the test suite"), or even disregard it entirely 😉

@xoofx xoofx merged commit 9f5e9ce into xoofx:main Mar 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants